//
//  UserDefinedCellForSubView2ForGetJobSheet.m
//  HondaPro_2
//
//  Created by Eric on 13-6-21.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "UserDefinedCellForSubView2ForGetJobSheet.h"

@interface UserDefinedCellForSubView2ForGetJobSheet ()

/** 收费区分 */
@property(nonatomic,weak)UILabel *chargeDiffLbl;
/** 项目代码 */
@property(nonatomic,weak)UILabel *projectCodeLbl;
/** 项目名称 */
@property(nonatomic,weak)UILabel *projectNameLbl;
/** 配件代码 */
@property(nonatomic,weak)UILabel *partCodeLbl;
/** 配件名称 */
@property(nonatomic,weak)UILabel *partNameLbl;
/** 价格 */
@property(nonatomic,weak)UILabel *priceLbl;
/** 数量 */
@property(nonatomic,weak)UILabel *countLbl;
/** 金额 */
@property(nonatomic,weak)UILabel *amountLbl;
/** 折扣 */
@property(nonatomic,weak)UILabel *discountLbl;

/** 收费区分列表 */
@property(nonatomic,strong)NSMutableDictionary *chargeDiff;

@property(nonatomic,strong)NSMutableArray *labelsArr;

@end

@implementation UserDefinedCellForSubView2ForGetJobSheet

@synthesize arrForAllLabelsKeysOnCell = _arrForAllLabelsKeysOnCell;
@synthesize dicForAllLabelsOnCell = _dicForAllLabelsOnCell;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        
        [self initLabelsOnCell];
    }
    return self;
}

-(void)setData:(NSDictionary *)data{
    _data = data;
    [self setNeedsLayout];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    if (self.data) {
        NSString *text = _data[@"CHARGE_PARTITION_CODE"];
        self.chargeDiffLbl.text = text = self.chargeDiff[text];
        
        self.projectCodeLbl.text = _data[@"LABOUR_CODE"];
        self.projectNameLbl.text = _data[@"LABOUR_NAME"];
        self.partCodeLbl.text = _data[@"PART_NO"];
        self.partNameLbl.text = _data[@"PART_NAME"];
        
        double num = [_data[@"PART_SALES_PRICE"] doubleValue];
        self.priceLbl.text = [NSString stringWithFormat:@"%.2f",num];
        self.countLbl.text = [_data[@"PART_QUANTITY"]stringValue];
        
        num = [_data[@"PART_SALES_AMOUNT"] doubleValue];
        self.amountLbl.text = [NSString stringWithFormat:@"%.2f",num];
//        self.discountLbl.text = [_data[@"DISCOUNT"]stringValue];
        self.selectionStyle = UITableViewCellSelectionStyleGray;
        
    }else{
        [self.labelsArr makeObjectsPerformSelector:@selector(setText:) withObject:nil];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    switch (self.row%2)
    {
        case 0:
            //浅灰色
            self.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            self.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            break;
            
        default:
            break;
    }
    CGFloat w=self.bounds.size.width/8;
    CGFloat h=self.bounds.size.height;
    self.chargeDiffLbl.frame = CGRectMake(0, 0, w, h);
    
    self.projectCodeLbl.frame =  CGRectMake(CGRectGetMaxX(self.chargeDiffLbl.frame), 0, w, h);
    
    self.projectNameLbl.frame =  CGRectMake(CGRectGetMaxX(self.projectCodeLbl.frame), 0, w, h);;
    
    self.partCodeLbl.frame = CGRectMake(CGRectGetMaxX(self.projectNameLbl.frame), 0, w, h);;
    
    self.partNameLbl.frame =  CGRectMake(CGRectGetMaxX(self.partCodeLbl.frame), 0, w, h);;
    
    self.priceLbl.frame =  CGRectMake(CGRectGetMaxX(self.partNameLbl.frame), 0, w, h);;
    
    self.countLbl.frame = CGRectMake(CGRectGetMaxX(self.priceLbl.frame), 0, w, h);;
    
    self.amountLbl.frame =  CGRectMake(CGRectGetMaxX(self.countLbl.frame), 0, w, h);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)initLabelsOnCell
{
    self.chargeDiffLbl = [self setupLabelWithPreFrame:CGRectZero lableWidth:70];
    
    self.projectCodeLbl = [self setupLabelWithPreFrame:self.chargeDiffLbl.frame lableWidth:100];
    
    self.projectNameLbl = [self setupLabelWithPreFrame:self.projectCodeLbl.frame lableWidth:230];
    
    self.partCodeLbl = [self setupLabelWithPreFrame:self.projectNameLbl.frame lableWidth:80];
    
    self.partNameLbl = [self setupLabelWithPreFrame:self.partCodeLbl.frame lableWidth:178];
    
    self.priceLbl = [self setupLabelWithPreFrame:self.partNameLbl.frame lableWidth:80];
    
    self.countLbl = [self setupLabelWithPreFrame:self.priceLbl.frame lableWidth:80];
    
    self.amountLbl = [self setupLabelWithPreFrame:self.countLbl.frame lableWidth:80];
    
//    self.discountLbl = [self setupLabelWithPreFrame:self.amountLbl.frame lableWidth:50];
    
}

-(UILabel *)setupLabelWithPreFrame:(CGRect)preFrame lableWidth:(CGFloat)lblW{
    CGFloat lblY = 0;
    CGFloat lblH = 50;
//    CGFloat margin = 0;
//    if (preFrame.origin.x == 0) {
//        margin = 0;
//    }
    CGFloat lblX = CGRectGetMaxX(preFrame);
    UILabel *lbl = [[UILabel alloc] init];
    lbl.frame = CGRectMake(lblX, lblY, lblW, lblH);
    lbl.backgroundColor = [UIColor clearColor];
    lbl.textColor = [UIColor grayColor];
    lbl.font = [UIFont fontWithName:@"ArialMT" size:16];
    lbl.numberOfLines = 2;
    lbl.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:lbl];
    [self.labelsArr addObject:lbl];
    return lbl;
}

-(NSMutableArray *)labelsArr{
    if (_labelsArr == nil) {
        _labelsArr = [NSMutableArray array];
    }
    return _labelsArr;
}

-(NSMutableDictionary *)chargeDiff{
    if (_chargeDiff == nil) {
        _chargeDiff = [NSMutableDictionary dictionary];
        NSMutableArray *arr = [self readershuDICimform][@"TM_CHARGE_PARTITION"];
        for (NSDictionary *dic in arr) {
            NSString *name = dic[@"NAME"];
            NSString *val = dic[@"CODE"];
            [_chargeDiff setObject:name forKey:val];
        }
    }
    return _chargeDiff;
}

-(NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    //nslog(@"%@",dic);
    return dic;
}

@end
